package com.briup.cloud.framework.resource.config.rest;

import com.briup.framework.utils.execption.BriupFrameworkException;
import com.briup.framework.utils.web.Response;
import com.briup.framework.utils.web.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @Author guomiao
 * @Date 2022/5/26 9:36
 * 定义这个类，用来对所有远程请求进行代理，如果远程接口返回的结果逻辑状态码是200，那么继续向下，否则抛出异常。
 */
@Component
@Aspect
@Slf4j
public class RestResponseAspect {

    public RestResponseAspect(){
        log.debug("这是远程服务调用的处理切面。。。。。。。");
    }

    @Pointcut("execution(public * com.briup..web.rest..*Service.*(..))")
    public void respontPoint(){
        log.debug("定义切入点。。");
    }

    @AfterReturning(pointcut = "respontPoint()",returning = "result")
    public void aroundRestRequest(JoinPoint jp,Object result){
      if(result instanceof Response){
          Response resp = (Response) result;
          if(resp.getCode().intValue()!=200){
              throw new BriupFrameworkException(resp.getMessage(),ResponseCode.ERROR);
          }
      }
    }
}
